<template>
  <div class='container'>
    <el-card class="box-card">
      <div class="app">
        <div class="left">
          <span class="name">标签名称</span>
          <el-input class="search" clearable v-model="q.tagName"></el-input>
          <span class="name" style='margin: 0 10px;'>状态</span>
          <el-select v-model="q.state" placeholder="请选择">
            <el-option v-for="(item,index) in status" :key="index" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
          <el-button size="small" style='margin-left: 10px;' @click="empty">清空</el-button>
          <el-button type="primary" size="small" @click="hSearch">搜索</el-button>
        </div>
        <div class="right">
          <el-button type="success" icon="el-icon-edit" size="small" @click="hAdd">
            新增标签
          </el-button>
        </div>
      </div>
      <div class="tips">
        <i class="el-icon-info" />
        <span class="amount">数据一共有{{counts}}条</span>
      </div>
      <!-- 表格 -->
      <el-table :data="list" style="width: 100%">
        <el-table-column type="index" label="序号">
        </el-table-column>
        <el-table-column prop="subjectName" label="所属学科">
        </el-table-column>
        <el-table-column prop="tagName" label="标签名称">
        </el-table-column>
        <el-table-column prop="username" label="创建者">
        </el-table-column>
        <el-table-column label="创建日期">
          <template v-slot="{row}">
            {{fnTime(row.addDate)}}
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template v-slot="{row}">
            {{'已'+ dataConversion(row.state)}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button type="text" @click="hForbidden(row)">{{row.state === 1? '禁用' : '启用'}}</el-button>
            <el-button :disabled="row.state !== 1" type="text" @click="hEdit(row.id)">修改</el-button>
            <el-button :disabled="row.state !== 1" type="text" @click="hDel(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="q.page" :page-sizes="[ 10, 20, 50]" :page-size="q.pagesize" background :total="counts" layout="prev, pager, next, sizes, jumper">
      </el-pagination>
    </el-card>
    <!-- 弹窗 -->
    <el-dialog :title="isEdit? '修改标签': '添加标签'" :close-on-click-modal="false" :close-on-press-escape="false" :visible.sync="dialogVisible" width="27%">
      <tagsDialog v-if="dialogVisible" ref="Dialog" @close='dialogVisible = false' :id='curId' :isEdit='isEdit' @upData="setCatalogueList" />
    </el-dialog>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { list, remove, changeState } from '@/api/hmmm/tags'
import tagsDialog from './tagsDialog.vue'
import { status } from '@/api/hmmm/constants'

const obj = {}
status.forEach(item => { obj[item.value] = item.label })
export default {
  components: {
    tagsDialog
  },
  data () {
    return {
      q: {
        page: 1,
        pagesize: 10,
        tagName: '',
        state: null
      },
      list: [],
      counts: 0,
      dialogVisible: false,
      isEdit: true,
      curId: 0,
      status: []
    }
  },
  created () {
    this.setCatalogueList()
  },
  methods: {
    // 获取标签列表
    async setCatalogueList () {
      try {
        const res = await list(this.q)
        this.list = res.data.items
        this.counts = res.data.counts
        this.status = status
      } catch (e) {
        console.log(e)
      }
    },
    // 时间转换
    fnTime (data) {
      return dayjs(data).format('YYYY-MM-DD HH:mm:ss')
    },
    // 数据转换
    dataConversion (data) {
      return obj[data]
    },
    // 每页条数
    handleSizeChange (pagesize) {
      this.q.pagesize = pagesize
      this.setCatalogueList()
    },
    // 当前页数
    handleCurrentChange (page) {
      this.q.page = page
      this.setCatalogueList()
      if (this.q.tagName !== '' && this.counts > 0 && page >= 2) {
        console.log(123)
        this.q.page = 1
        this.setCatalogueList()
      }
    },
    // 删除
    async hDel (row) {
      const str = await this.$confirm('确定要删除吗?', '提示', { type: 'warning' }).catch(e => e)
      if (str !== 'confirm') return
      try {
        await remove(row.id)
        this.$message.success('删除成功')
        if (this.list.length === 1 && this.q.page > 1) this.q.page--
        this.setCatalogueList()
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 添加
    hAdd () {
      this.isEdit = false
      this.dialogVisible = true
    },
    // 修改
    hEdit (id) {
      this.curId = id
      this.isEdit = true
      this.dialogVisible = true
    },
    // 禁用
    async hForbidden (row) {
      row.state === 1 ? row.state = 0 : row.state = 1
      try {
        await changeState(row)
        this.$message.success('操作成功')
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 清空input
    empty () {
      this.q = {
        page: 1,
        pagesize: 10,
        tagName: '',
        state: null
      }
      this.setCatalogueList()
    },
    // 搜索
    hSearch () {
      if (this.q.tagName === '' && this.q.state === null) {
        return
      } else {
        this.q.page = 1
        this.setCatalogueList()
      }
    }
  }
}
</script>

<style scoped>
.app {
  display: flex;
  justify-content: space-between;
}
.box-card {
  margin: 10px;
}
.name {
  font-weight: 700;
  color: #60627f;
  font-size: 14px;
}
.search {
  width: 190px;
  margin: 0 10px;
}
.tips {
  line-height: 34px;
  margin: 15px 0;
  padding: 0px 15px;
  border-radius: 5px;
  background: #f4f4f5;
}
i {
  margin-right: 10px;
  color: #909399;
}
.amount {
  font-size: 13px;
  color: #909399;
}
.el-pagination {
  display: flex;
  margin-top: 20px;
  justify-content: flex-end;
}
</style>
